<template>
  <el-dialog title="冲账" :visible.sync="chongzhangVisible" width="50%">
    <el-tabs type="border-card" @tab-click="tabChange" v-model="activeName">
      <el-tab-pane name="4" label="房费">
        <el-table :data="roomCostList" height="250" border style="width: 100%" class="table-list">
          <el-table-column prop="payType" label="费用名称">
            <template slot-scope="scope">
              <span v-if="scope.row.payType == 4">
                <span v-if="scope.row.isNightTrial == 1">夜审房费</span>
                <span v-else>当日房费</span>
              </span>
            </template>
          </el-table-column>
          <el-table-column label="备注">
            <template slot-scope="scope">
              {{scope.row.remarks ? scope.row.remarks : '--'}}
            </template>
          </el-table-column>
          <el-table-column prop="payPrice" label="金额">
            <template slot-scope="scope">
              {{scope.row.payPrice-scope.row.hcMoney}}
            </template>
          </el-table-column>
          <el-table-column label="冲账金额">
            <template slot-scope="scope">
              <el-input-number size="small" v-model.number="scope.row.surplusMoney" :min="0" @blur="validate(scope.row.payPrice-scope.row.hcMoney,$event)"
                :max="scope.row.payPrice-scope.row.hcMoney" :controls="false" :precision="2"></el-input-number>
            </template>
          </el-table-column>
          <el-table-column label="冲账原因">
            <template slot-scope="scope">
              <el-input type="textarea" :rows="2" placeholder="请输入冲账原因" v-model="scope.row.accountyy">
              </el-input>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
      <el-tab-pane name="2" label="消费">
        <el-table :data="roomCostList" height="250" border style="width: 100%" class="table-list">
          <el-table-column prop="consumeInfo" label="费用名称">
            <template slot-scope="scope">
              {{costMap[scope.row.payType]}}
            </template>
          </el-table-column>
          <el-table-column prop="payPrice" label="金额">
            <template slot-scope="scope">
              {{scope.row.payPrice-scope.row.hcMoney}}
            </template>
          </el-table-column>
          <el-table-column label="冲账金额">
            <template slot-scope="scope">
              <el-input-number v-model.number="scope.row.surplusMoney" :min="0" @blur="validate(scope.row.payPrice-scope.row.hcMoney,$event)"
                :max="scope.row.payPrice-scope.row.hcMoney" :controls="false" :precision="2"></el-input-number>
            </template>
          </el-table-column>
          <el-table-column label="冲账原因">
            <template slot-scope="scope">
              <el-input type="textarea" :rows="2" placeholder="请输入冲账原因" v-model="scope.row.accountyy">
              </el-input>
            </template>

          </el-table-column>
        </el-table>
      </el-tab-pane>
    </el-tabs>



    <span slot="footer" class="dialog-footer">
      <el-button @click="chongzhangVisible = false">取 消</el-button>
      <el-button type="primary" @click="cz()">确认冲账</el-button>
    </span>
  </el-dialog>
</template>
<script>
  export default {
    data() {
      return {
        chongzhangVisible: false,
        activeName: '4',
        roomCostList: [], //产生的房费列表
        costMap: {
          4: '当日房费',
          2: '商品消费'
        },
        people: {}
      };
    },
    methods: {
      init(people) {
        this.people = people
        this.chongzhangVisible = true
        this.getList(this.activeName)
      },
      getList(payType) {
        this.roomCostList = [], //产生的房费列表
          this.$http({
            url: this.$http.adornUrl(`/hotel/thotelfloorroomcheckinaccounting/getByTypeWithCZ`),
            method: 'get',
            params: this.$http.adornParams({
              payType: payType,
              peopleId: this.people.id
            })
          }).then(({
            data
          }) => {
            if (data && data.code === 0) {
              console.log(data)
              this.roomCostList = data.list;
            }
          });
      },
      tabChange(tab, event) {
        this.getList(tab.name)
      },
      validate(max, e) {
        if (parseFloat(e.target.value) > max) {
          this.$message.error("请输入一个小于" + max + "的数，已设为最大值");
        }
      },
      cz() {
        console.log(this.roomCostList)
        var flag = false;
        this.roomCostList.forEach(item => {
          if (item.surplusMoney > 0) {
            flag = true;
          }
        })

        if (!flag) {
          this.$message.error("至少一个不为0");
          return
        }

        this.$http({
          url: this.$http.adornUrl(`/hotel/thotelfloorroomcheckinaccounting/cz`),
          method: 'post',
          data: this.$http.adornData(this.roomCostList, false)

        }).then(({
          data
        }) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.chongzhangVisible = false
                this.$emit("refreshDataList");
              }
            });
          } else {
            this.$message.error(data.msg);
          }
        });
      }
    },
  };
</script>
<style>
  .checkin_info_summary .table-list table tr th:last-of-type {
    border-top-right-radius: 4px;
    border-top-left-radius: 4px;
    background: #fff;
    color: #666;
  }

  .checkin_info_summary .table-list table tr td:last-of-type {
    border-bottom-right-radius: 4px;
    border-bottom-left-radius: 4px;
    background: #fff;
    color: #000;
  }
</style>
